package com.xiaoyi.ui.activity;

import android.animation.ValueAnimator;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.FrameLayout;
import android.widget.ImageView;

import com.avin.yi.R;
import com.bumptech.glide.Glide;
import com.xiaoyi.tool.MyDeviceInforTool;

import cn.hzw.doodle.DoodleOnTouchGestureListener;
import cn.hzw.doodle.DoodleParams;
import cn.hzw.doodle.DoodlePen;
import cn.hzw.doodle.DoodleShape;
import cn.hzw.doodle.DoodleTouchDetector;
import cn.hzw.doodle.DoodleView;
import cn.hzw.doodle.IDoodleListener;
import cn.hzw.doodle.core.IDoodle;
import cn.hzw.doodle.core.IDoodleSelectableItem;
import cn.hzw.doodle.core.IDoodleTouchDetector;


public class DoodleActivity extends AppCompatActivity {

    private DoodleView mDoodleView;
    private DoodleOnTouchGestureListener mTouchGestureListener;
    private Context context ;
    ImageView backImg ;
    private static final String KEY_URL = "KEY_URL" ;

    public static void startInceTance(String url , Context context){
        Intent intent = new Intent(context , DoodleActivity.class) ;
        intent.putExtra( KEY_URL ,url) ;
        context.startActivity(intent);
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(cn.hzw.doodle.R.layout.doodle_layout);
        if(getSupportActionBar()!=null){
            getSupportActionBar().hide();
        }
        context = this ;


        backImg  = findViewById(R.id.backImg) ;
       String  url = getIntent().getStringExtra(KEY_URL   ) ;
        Glide.with(context).load(url) .into(backImg);


        findViewById(R.id.goBack).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ((Activity)context ).finish();
            }
        });

        findViewById(R.id.hiden).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(backImg.getVisibility() == View.VISIBLE){
                    backImg.setVisibility(View.INVISIBLE);
                }else {
                    backImg.setVisibility(View.VISIBLE);
                }
            }
        });

        int width = MyDeviceInforTool.getWidth(this);
        int height = MyDeviceInforTool.getHeight(this) ;
        Bitmap bitmap =  Bitmap.createBitmap(width , height ,Bitmap.Config. ARGB_8888) ;
        FrameLayout mFrameLayout =   findViewById(cn.hzw.doodle.R.id.doodle_container);


        IDoodleListener iDoodleListener = new IDoodleListener() {
            @Override
            public void onSaved(IDoodle doodle, Bitmap doodleBitmap, Runnable callback) {

            }

            @Override
            public void onReady(IDoodle doodle) {

            }
        };

        mDoodleView = new DoodleView(this, bitmap,   Color.RED , iDoodleListener, null);

        mTouchGestureListener = new DoodleOnTouchGestureListener(mDoodleView, new DoodleOnTouchGestureListener.ISelectionListener() {
            @Override
            public void onSelectedItem(IDoodle doodle, IDoodleSelectableItem selectableItem, boolean selected) {

            }

            @Override
            public void onCreateSelectableItem(IDoodle doodle, float x, float y) {

            }
        });

        IDoodleTouchDetector detector = new DoodleTouchDetector( context, mTouchGestureListener);
        mDoodleView.setDefaultTouchDetector(detector);
        // 涂鸦参数
        DoodleParams mDoodleParams = new DoodleParams();
        mDoodleParams.mIsFullScreen = true;
        mDoodleParams.mPaintUnitSize = DoodleView.DEFAULT_SIZE;
        mDoodleView.setIsDrawableOutside(mDoodleParams.mIsDrawableOutside);
        mDoodleView.setDoodleMinScale(mDoodleParams.mMinScale);
        mDoodleView.setDoodleMaxScale(mDoodleParams.mMaxScale);
        mDoodleView.setSize(8);
        mDoodleView.setShowOriginal(false);
        mDoodleView.setPen(DoodlePen.BRUSH);
        mDoodleView.setShape(DoodleShape.HAND_WRITE);
        mFrameLayout.addView(mDoodleView, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
    }



    public void onClick(final View v) {
       if (v.getId() == cn.hzw.doodle.R.id.btn_undo) {
           mDoodleView.undo();
        }
    }


}
